package com.object.oriented.exam.system2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.oriented.exam.system1.entity.Result;
import com.object.oriented.exam.system2.dao.ProblemPropertyMapping;
import com.object.oriented.exam.system2.entity.ProblemProperty;
import com.object.oriented.exam.system2.service.ProblemPropertyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Lanh
 * @date 2023.06.19 20:19
 **/

@Service
public class ProblemPropertyServiceImpl extends ServiceImpl<ProblemPropertyMapping, ProblemProperty> implements ProblemPropertyService {

    @Override
    @Transactional
    public Result insert(ProblemProperty problemProperty) {
        int res = baseMapper.insert(problemProperty);
//        if (res == 0){
//            Result.fail("更新失败;");
//        }
        return Result.success();
    }

    @Override
    @Transactional
    public Result update(ProblemProperty problemProperty) {
        int res = baseMapper.updateById(problemProperty);

//        if (res == 0){
//            Result.fail("更新失败;");
//        }
        return Result.success();
    }

    @Override
    public Result getProperty(Integer questionId) {
        QueryWrapper<ProblemProperty> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id",questionId);
        ProblemProperty problemProperty = baseMapper.selectOne(queryWrapper);
//        if (problemProperty == null){
//            return Result.fail("");
//        }
        return Result.success(problemProperty);
    }

    @Override
    public Result listProperty() {
        List<ProblemProperty> problemProperties = baseMapper.selectList(new QueryWrapper<>());
        return Result.success(problemProperties);
    }

    @Override
    public Result delete(ProblemProperty problemProperty) {
        int res = baseMapper.deleteById(problemProperty);
        return Result.success();
    }


}
